package weaver.interfaces.zhlx.duss.action.financemodule.t6;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.T6Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 项目用款申请领导审批节点提交后调用资金系统提供推送付款信息（外汇）接口推送资金系统
 * @ModifyLog:
 **/
public class WeaXmyksqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXmyksqlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用资金系统接口返回:";
            JSONArray paybillList = new JSONArray();
            JSONArray saveForeignExchangeList = new JSONArray();
            JSONArray saveLcEnrolList = new JSONArray();
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String zffs = Util.null2String(rs.getString("zffs"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String sqrq = Util.null2String(rs.getString("sqrq"));
            String jbrwb = Util.null2String(rs.getString("jbrwb"));
            String qwzfrq = Util.null2String(rs.getString("qwzfrq"));
            String skdwmc = Util.null2String(rs.getString("skdwmc"));
            String kzlb = Util.null2String(rs.getString("kzlb"));
            String skdwgb = Util.null2String(rs.getString("skdwgb"));
            String skdwmcyw = Util.null2String(rs.getString("skdwmcyw"));
            String khxyw = Util.null2String(rs.getString("khxyw"));
            String swiftcode = Util.null2String(rs.getString("swiftcode"));
            String khxdzyw = Util.null2String(rs.getString("khxdzyw"));
            String khx = Util.null2String(rs.getString("khx"));
            String lxh = Util.null2String(rs.getString("lxh"));
            String zh = Util.null2String(rs.getString("zh"));
            String kzrq = Util.null2String(rs.getString("kzrq"));
            String xyzyxq = Util.null2String(rs.getString("xyzyxq"));

            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                //币种
                String bz = Util.null2String(rs.getString("bz"));
                //资金计划行号
                String zjjhxh = Util.null2String(rs.getString("zjjhxh"));
                //本次请款金额-原币
                String bcqkjeyb = Util.null2String(rs.getString("bcqkjeyb"));
                //本次请款金额-本币
                String bcqkjebb = Util.null2String(rs.getString("bcqkjebb"));
                //单位编码
                String gsdm = Util.null2String(rs.getString("gsdm"));
                String pldb = Util.null2String(rs.getString("pldb"));
                String zjmxxmwb = Util.null2String(rs.getString("zjmxxmwb"));
                JSONObject paybillJson = new JSONObject();
                paybillJson.put("EC_NO", lcbh);
                paybillJson.put("EC_ID", requestid);
                paybillJson.put("FROM_ID", formid);
                paybillJson.put("BUSINESS_DATE", sqrq);
                paybillJson.put("BUSINESS_USER", jbrwb);
                paybillJson.put("WISHPAY_DATE", qwzfrq);
                paybillJson.put("ABS", zjmxxmwb);
                paybillJson.put("PAYEE_ACC_NAME", skdwmc);
                paybillJson.put("PAYEE_BANK", khx);
                paybillJson.put("PAYEE_BANK_CODE", lxh);
                paybillJson.put("PAYEE_ACC_NO", zh);
                paybillJson.put("VOUCHER_TYPE", "3".equals(zffs) ? "70" : "06");
                paybillJson.put("CUR", bz);
                paybillJson.put("SERIAL_ID", id);
                paybillJson.put("DETAIL_ID", zjjhxh);
                paybillJson.put("AMOUNT_Y", bcqkjeyb);
                paybillJson.put("AMOUNT_B", bcqkjebb);
                paybillJson.put("DATA_SOURCE", "EC");
                paybillJson.put("CORP_CODE", gsdm);
                paybillJson.put("IS_BATCH", pldb);
                paybillList.add(paybillJson);
                JSONObject saveForeignExchangeJson = new JSONObject();
                saveForeignExchangeJson.put("EC_NO", lcbh);
                saveForeignExchangeJson.put("EC_ID", requestid);
                saveForeignExchangeJson.put("FROM_ID", formid);
                saveForeignExchangeJson.put("BUSINESS_DATE", sqrq);
                saveForeignExchangeJson.put("BUSINESS_USER", jbrwb);
                saveForeignExchangeJson.put("WISHPAY_DATE", qwzfrq);
                saveForeignExchangeJson.put("ABS", zjmxxmwb);
                saveForeignExchangeJson.put("PAYEE_ACC_NAME", skdwmc);
                saveForeignExchangeJson.put("PAYEE_BANK", khx);
                saveForeignExchangeJson.put("PAYEE_BANK_CODE", lxh);
                saveForeignExchangeJson.put("PAYEE_ACC_NO", zh);
                saveForeignExchangeJson.put("PAYEE_COUNTRY_CODE", skdwgb);
                saveForeignExchangeJson.put("PAYEE_ACC_NAME_EN", skdwmcyw);
                saveForeignExchangeJson.put("PAYEE_BANK_EN", khxyw);
                saveForeignExchangeJson.put("SWIFT_CODE", swiftcode);
                saveForeignExchangeJson.put("PAYEE_ADDRESS", khxdzyw);
                saveForeignExchangeJson.put("SERIAL_ID", id);
                saveForeignExchangeJson.put("CUR", bz);
                saveForeignExchangeJson.put("DETAIL_ID", zjjhxh);
                saveForeignExchangeJson.put("AMOUNT_Y", bcqkjeyb);
                saveForeignExchangeJson.put("AMOUNT_B", bcqkjebb);
                saveForeignExchangeJson.put("DATA_SOURCE", "EC");
                saveForeignExchangeJson.put("CORP_CODE", gsdm);
                saveForeignExchangeList.add(saveForeignExchangeJson);
                JSONObject saveLcEnrolJson = new JSONObject();
                saveLcEnrolJson.put("EC_NO", lcbh);
                saveLcEnrolJson.put("EC_ID", requestid);
                saveLcEnrolJson.put("FROM_ID", formid);
                saveLcEnrolJson.put("BUSINESS_DATE", sqrq);
                saveLcEnrolJson.put("BUSINESS_USER", jbrwb);
                saveLcEnrolJson.put("WISHPAY_DATE", qwzfrq);
                saveLcEnrolJson.put("ABS", zjmxxmwb);
                saveLcEnrolJson.put("PAYEE_ACC_NAME", skdwmc);
                saveLcEnrolJson.put("PAYEE_BANK", khx);
                saveLcEnrolJson.put("PAYEE_BANK_CODE", lxh);
                saveLcEnrolJson.put("PAYEE_ACC_NO", zh);
                saveLcEnrolJson.put("LC_INTER", kzlb);
                saveLcEnrolJson.put("ISSUING_DATE", kzrq);
                saveLcEnrolJson.put("EXPIRY_DATE", xyzyxq);
                saveLcEnrolJson.put("SERIAL_ID", id);
                saveLcEnrolJson.put("CUR", bz);
                saveLcEnrolJson.put("DETAIL_ID", zjjhxh);
                saveLcEnrolJson.put("AMOUNT_Y", bcqkjeyb);
                saveLcEnrolJson.put("AMOUNT_B", bcqkjebb);
                saveLcEnrolJson.put("DATA_SOURCE", "EC");
                saveLcEnrolJson.put("CORP_CODE", gsdm);
                saveLcEnrolList.add(saveLcEnrolJson);
            }
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaXmyksqlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            if ("1".equals(zffs) || "3".equals(zffs)) {
                writeLog(">>>>>WeaXmyksqlcAction>>>>>paybillList=" + paybillList);
                JSONObject resultJson = T6Util.payBill(paybillList, dataMap);
                writeLog(">>>>>WeaXmyksqlcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            rs.executeQuery("update " + tablename + "_dt1 set type=? where mainid=? and id=?", code1, mainid, serialId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            if ("2".equals(zffs)) {
                writeLog(">>>>>WeaXmyksqlcAction>>>>>saveForeignExchangeList=" + saveForeignExchangeList);
                JSONObject resultJson = T6Util.saveForeignExchange(saveForeignExchangeList, dataMap);
                writeLog(">>>>>WeaXmyksqlcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            rs.executeQuery("update " + tablename + "_dt1 set type=? where mainid=? and id=?", code1, mainid, serialId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            if ("0".equals(zffs)) {
                writeLog(">>>>>WeaXmyksqlcAction>>>>>saveLcEnrolList=" + saveLcEnrolList);
                JSONObject resultJson = T6Util.saveLcEnrol(saveLcEnrolList, dataMap);
                writeLog(">>>>>WeaXmyksqlcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            rs.executeQuery("update " + tablename + "_dt1 set type=? where mainid=? and id=?", code1, mainid, serialId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXmyksqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}